﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace CommonUtils
{
    /// <summary>
    /// https://blog.csdn.net/fromfire2/article/details/102605174
    /// </summary>
    public static class SwaggerUtil
    {
        //添加Header到http请求
        public static void AddHeader(this SwaggerGenOptions options, string name)
        {
            var id = "添加Header到http请求";
            //添加Authorize按钮
            options.AddSecurityDefinition(id, new OpenApiSecurityScheme()
            {
                Description = id,
                Name = name,
                In = ParameterLocation.Header,
                Type = SecuritySchemeType.ApiKey,
            });
            //注入Header到Http请求
            options.AddSecurityRequirement(new OpenApiSecurityRequirement { {
            new OpenApiSecurityScheme { Reference = new OpenApiReference{
            Id = id,Type = ReferenceType.SecurityScheme}},
            new string[0]}});
        }

        public static void JsonFormat(this IServiceCollection services)
        {
            services.AddControllers()
            //原生Json服务中文、变量、注释支持差，这里切换为Newtonsoft提供的Json服务，必须引用Microsoft.AspNetCore.Mvc.NewtonsoftJson，引用Newtonsoft.Json此处无效
            .AddNewtonsoftJson(options =>
            {
                //保持Json属性/变量大小写
                options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                //忽略循环引用
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                //设置时间格式
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                //如字段为null值，该字段不会返回到前端
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });
        }

        public static void AddMySwagger(this IServiceCollection services)
        {
            services.JsonFormat();
            //生成swagger
            services.AddSwaggerGen(options =>
            {
                //开启swagger特性
                options.EnableAnnotations();
                //设置swagger标题
                //options.SwaggerDoc("doc", new OpenApiInfo { Title = "@Title", Version = "v1.0" });
                //添加接口注释//开启生成API文档才可使用
                if (FileUtil.Exists("comment.xml"))
                    options.IncludeXmlComments("comment.xml");
            });
        }

        public static void AddMySwagger2(this IServiceCollection services)
        {
            services.JsonFormat();
            //生成swagger
            services.AddSwaggerGen(options =>
            {
                //开启swagger特性
                options.EnableAnnotations();
                //设置swagger标题
                options.SwaggerDoc("doc", new OpenApiInfo { Title = "@Title", Version = "v1.0" });
                //添加接口注释//开启生成API文档才可使用
                if (FileUtil.Exists("comment.xml"))
                    options.IncludeXmlComments("comment.xml");
            });
        }

        //晚了: read-only can not be changed!
        //app.GetServices().AddSwaggerGen(options => options.EnableAnnotations());

        public static void UseMySwagger(this WebApplication app)
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        public static void UseMySwagger2(this WebApplication app)
        {
            app.UseSwagger();
            app.UseSwaggerUI(options => options.SwaggerEndpoint("/swagger/doc/swagger.json", null));
        }

        public static void OpenSwaggerDoc(this WebApplication app)
        {
            //添加接口注释//开启生成API文档才可使用
            if (FileUtil.Exists("comment.xml"))
                ProcessUtil.Open("comment.xml");
            ProcessUtil.Open(app.Url() + "/swagger/v1/swagger.json");//默认
            ProcessUtil.Open(app.Url() + "/swagger/doc/swagger.json");//自定义
        }

        public static void OpenSwagger(this WebApplication app)
        => ProcessUtil.Open(app.Url() + "/swagger/index.html");

    }
}
